package org.jeecg.modules.param.pile.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.common.service.CommonMethodService;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.pile.entity.BikePile;
import org.jeecg.modules.param.pile.service.IBikePileService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.param.pile.vo.PileCodeRsp;
import org.jeecg.modules.param.station.entity.BikeStation;
import org.jeecg.modules.param.station.mapper.BikeStationMapper;
import org.jeecg.modules.param.station.service.IBikeStationService;
import org.jeecg.modules.utils.BikeResult;
import org.jeecg.modules.utils.RandomCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: bike_pile
 * @Author: 月月鸟
 * @Date:   2021-05-19
 * @Version: V1.0
 */
@Api(tags="车桩设置")
@RestController
@RequestMapping("/pile/bikePile")
@Slf4j
public class BikePileController extends JeecgController<BikePile, IBikePileService> {
	@Autowired
	private IBikePileService bikePileService;
	@Autowired
	private CommonMethodService commonMethodService;
	@Resource
	private BikeStationMapper bikeStationMapper;
	 @Value("${jeecg.path.upload}")
	 public String upload;
	 @Value("${jeecg.path.scan}")
	 public String scan;

	/**
	 * 分页列表查询
	 *
	 * @param bikePile
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="车桩设置-分页列表查询", notes="车桩设置-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BikePile bikePile,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		if (StringUtils.isNotEmpty(bikePile.getStationId())){
			List<String> list = commonMethodService.getChildStation(bikePile.getStationId());
			if (!list.isEmpty()){
				bikePile.setStationArray(list);
			}else {
				return Result.OK();
			}
		}
		return bikePileService.pageList(pageNo,pageSize,bikePile);
	}

	/**
	 *   添加
	 *
	 * @param bikePile
	 * @return
	 */
	@AutoLog(value = "车桩设置-添加")
	@ApiOperation(value="车桩设置-添加", notes="车桩设置-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BikePile bikePile) {
		bikePile.setCode(RandomCodeUtils.getRandomCode("pile"));
		bikePileService.save(bikePile);
		return Result.OK("添加成功！");
	}
	 // TODO: 2021/5/24 缺少下载语音接口
	 /**
	  * 生成二维码
	  * */
	 @GetMapping(value = "/getCode")
	 @ApiOperation(value="生成二维码",notes="生成二维码")
	 public Result<?> getCode(String number , HttpServletResponse response) throws IOException {
		 String path = bikePileService.getZipCode(number, response);
		 return Result.ok(path);
	 }
	 /**
	  * 获取二维码Zip
	  * */
	 @GetMapping(value = "/getCodeZip")
	 @ApiOperation(value="生成二维码",notes="生成二维码")
	 public void getCodeZip(String path , HttpServletResponse response) throws IOException {
		 // 设置响应流信息
		 response.setContentType("application/ostet-stream");
		 response.setHeader("Pragma", "no-cache");
		 response.setHeader("Cache-Control", "no-cache");
		 response.setDateHeader("Expires", 0);
		 ServletOutputStream outputStream = response.getOutputStream();
		 bikePileService.toZip(upload+path,outputStream,true);
		 outputStream.close();
	 }

	 @GetMapping(value = "/sweepCode")
	 @ApiOperation(value="判断扫码途径",notes="判断扫码途径")
	 public BikeResult sweepCode(HttpServletRequest request, HttpServletResponse response, @RequestParam String code) throws IOException {
		 JSONObject jsonObject= new JSONObject();
		 String userAgent = request.getHeader("user-agent");
		 if (userAgent !=null && userAgent.contains("JiangNingBike")){
			 jsonObject.put("data",code);
			 return BikeResult.ok("1","成功",jsonObject);
		 }else {
			 response.sendRedirect(scan);
			 return BikeResult.ok("0","未识别",null);
		 }
	 }
	 /**
      *  编辑
      *
      * @param bikePile
      * @return
      */
	@AutoLog(value = "车桩设置-编辑",operateType = 3)
	@ApiOperation(value="车桩设置-编辑", notes="车桩设置-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BikePile bikePile) {
		bikePileService.updateById(bikePile);
		return Result.OK("编辑成功!");
	}
	 /**
	  *  是否启用
	  *
	  * @param bikePile
	  * @return
	  */
	 @AutoLog(value = "车桩管理-是否启用",operateType = 3)
	 @ApiOperation(value="车桩管理-是否启用", notes="车桩管理-是否启用")
	 @PutMapping(value = "/updateStatus")
	 public Result<?> updateStatus(@RequestBody BikePile bikePile) {
		 return bikePileService.updateStatus(bikePile);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @param stationId
	  * @return
	  */
	 @ApiOperation(value="车桩模糊列表", notes="车桩模糊列表")
	 @DeleteMapping(value = "/likeList")
	 public Result<?> queryPageList(@RequestParam String stationId) {
	 	if (StringUtils.isNotEmpty(stationId)){
			QueryWrapper<BikePile> wrapper = new QueryWrapper<>();
			wrapper.select("id","code");
			wrapper.eq("station_id",stationId);
			List<BikePile> list = bikePileService.list(wrapper);
			return Result.OK(list);
		}else {
	 		return Result.OK();
		}
	 }
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "车桩设置-通过id删除")
	@ApiOperation(value="车桩设置-通过id删除", notes="车桩设置-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		bikePileService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "车桩设置-批量删除")
	@ApiOperation(value="车桩设置-批量删除", notes="车桩设置-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.bikePileService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="车桩设置-通过id查询", notes="车桩设置-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BikePile bikePile = bikePileService.getById(id);
		if(bikePile==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(bikePile);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param bikePile
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BikePile bikePile) {
        return bikePileService.exportXls(request,bikePile);
    }


}
